package org.jweb.demo.controller;

import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.util.StringUtil;
import org.jweb.core.web.controller.BaseController;
import org.jweb.demo.service.DemoGoodsServiceI;
import org.jweb.demo.trans.form.DemoGoodsSaveForm;
import org.jweb.demo.trans.form.DemoGoodsSearchForm;
import org.jweb.demo.trans.vo.DemoGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo/demoGoodsAPI")
public class DemoGoodsController extends BaseController {
	@Autowired
	private DemoGoodsServiceI demoGoodsService;
	
	@RequestMapping(params = "list")
	@ResponseBody
	public ReplyDataMode list(DemoGoodsSearchForm searchForm){
		ReplyDataMode j = new ReplyDataMode();
		
		searchForm.emptyValue2NullValue();
		PageMode<DemoGoodsVO> voli = this.demoGoodsService.list(searchForm,1,10);
		
		j.setSuccess(true);
		j.setData(voli);
		
		return j;
	}
	
	@RequestMapping(params = "get")
	@ResponseBody
	public ReplyDataMode get(String goodsId){
		ReplyDataMode j = new ReplyDataMode();
		
		DemoGoodsVO vo= this.demoGoodsService.get(goodsId);
		
		j.setSuccess(true);
		j.setData(vo);
		
		return j;
	}
	
	@RequestMapping(params = "saveForm")
	@ResponseBody
	public ReplyDataMode saveForm(DemoGoodsSaveForm saveForm){
		ReplyDataMode j = new ReplyDataMode();
		
		if(StringUtil.isNotEmpty(saveForm.getId())){
			this.demoGoodsService.update(saveForm);
			
		} else {
			this.demoGoodsService.add(saveForm);
		}
		
		
		j.setSuccess(true);
		
		return j;
	}
	
	
	@RequestMapping(params = "saveEntity")
	@ResponseBody
	public ReplyDataMode saveEntity(DemoGoodsSaveForm saveForm){
		ReplyDataMode j = new ReplyDataMode();
		
		if(StringUtil.isNotEmpty(saveForm.getId())){
			this.demoGoodsService.update(saveForm);
			
		} else {
			this.demoGoodsService.add(saveForm);
		}
		
		
		j.setSuccess(true);
		
		return j;
	}
	
	
	
	
}
